$(function () {
    $(".single_deploy").on('click', single_deploy);
    $(".single_check_log").on('click', check_log);
    $("#batch_deploy_button").on('click', batch_deploy)
});

function single_deploy(event) {
    let index = $(this).data("value");
    let release_version = $("#gittag_" + index).val();
    let url = "/deploy/pre_single_deploy";
    ajax_build_job(url, release_version, $(this), index);
}


function ajax_build_job(url, data_param, button_instance, index) {
    alert("部署 release_version: " + data_param);
    $.post(
        url,
        {release_version: data_param},
        function (data) {
            if ((data['code']) == "200") {
                button_instance.attr("disabled", "true");
                query_deploy_state(data["job_name"], data["build_number"], data["queue_item"], index)
            }
            if ((data['code']) == "999") {
                alert(data_param + " deploy 失败!");
            }

        },
        "json"
    );
}


function query_deploy_state(job_name, build_number, queue_item, index) {
    let url = "/deploy/query_deploy_state"
    $.getJSON(
        url,
        {
            "job_name": job_name,
            "build_number": build_number,
            "queue_item": queue_item
        },
        function (data) {
            let progress;
            // set status text

            // if data status is finished, adjust progress according to response status.
            if (data == null || data['Status'] == "Finished") {
                progress = 100;
                if (data["Response"] == "SUCCESS") {
                    progress = 100;
                    $("#progress_bar_" + index).addClass("bg-success");
                }
                if (data["Response"] == "FAILED") {
                    progress = 0;
                    $("#progress_bar_" + index).addClass("bg-danger");
                }
                $("#progress_bar_" + index).attr("aria-valuenow", progress);
                $("#progress_bar_" + index).css("width", progress + "%");
                $("#progress_bar_" + index).removeClass("progress-bar-animated");
                $("#progress_bar_" + index).removeClass("progress-bar-striped");
                $("#progress_bar_" + index).text(data['Status'] + ":" + data["Response"])

            }
            // else,adjust progress according to response value.and invoke by setTimeout again.
            else if (data['Status'] == "Building" || data['Status'] == "Waiting") {
                if (data['Status'] == "Waiting") {
                    progress = 0;
                }
                if (data['Status'] == "Building") {
                    progress = data["Response"];
                }
                $("#progress_bar_" + index).attr("aria-valuenow", progress)
                $("#progress_bar_" + index).css("width", progress + "%")
                $("#progress_bar_" + index).text(data['Status'] + ":" + data["Response"] + "%")
                setTimeout(query_deploy_state, 1000 * 3, job_name, build_number, queue_item, index);
            }
        }
    )

}


function check_log(event) {
    let index = $(this).data("value");
    let release_version = $("#gittag_" + index).val();
    let sysidRe = /[lL][dD](\d)*/;
    let sysid = sysidRe.exec(release_version)[0];
    let device_index = $(this).data("deviceIndex");
    let env = "pre"
    let url = "/deploy/query_zeus_deploy_info";
    $.getJSON(
        url,
        {
            "sys_id": sysid,
            "env": env
        },
        function (data) {
            if (data['code'] == "200" && Array.isArray(data['deploy_info']) && data['deploy_info'].length) {
                let device_info = data['deploy_info'][device_index]
                let ip = device_info['sysName']
                let tomcat_dir = device_info['deployPath']
                let log_path = device_info['logPath']
                invoke_log_window(ip, tomcat_dir, log_path)
            } else {
                alert(release_version + ": ZEUS 设备查询无数据!");
            }
        }
    )
}

function invoke_log_window(ip, tomcat_dir, log_path) {
    let log_api_url = "http://10.34.30.229/flushpage/?ip=" + ip + "&tomdir=" + tomcat_dir + "&serlog=" + log_path
    let w = window.open(log_api_url);
    w.document.title = 'testing';
}

//function to batch deploy the job list in page.
function batch_deploy(event) {
    console.log("get in batch deploy");
    let url = "/deploy/pre_single_deploy";
    $(".single_deploy").each(function (i, single_button) {
        console.log("now the i is:" + i + " button is " + single_button);
        let index = $(single_button).data("value");
        let release_version = $("#gittag_" + index).val();
        ajax_build_job(url, release_version, $(this), index);
    });

}